package com.h5ve.toolkit.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.h5ve.base.api.R;
import com.h5ve.base.util.StrUtil;
import com.h5ve.data.util.ExcelUtil;
import com.h5ve.log.annotation.SysLog;
import com.h5ve.toolkit.entity.Table;
import com.h5ve.toolkit.entity.Trigger;
import com.h5ve.toolkit.service.TableService;
import com.h5ve.toolkit.service.TriggerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author h5ve
 * @date 2025-03-17 19:22:58
 * @description 触发器Controller
 */
@Api("触发器管理")
@RestController
@AllArgsConstructor
@RequestMapping("/toolkit/trigger")
public class TriggerController {

    private final TriggerService triggerService;
    private final TableService tableService;

    private LambdaQueryWrapper<Trigger> getQueryWrapper(Trigger trigger) {
        LambdaQueryWrapper<Trigger> queryWrapper = new LambdaQueryWrapper<Trigger>()
                .like(StrUtil.isNotBlank(trigger.getName()), Trigger::getName, trigger.getName())
                .eq(trigger.getEntityId() != null, Trigger::getEntityId, trigger.getEntityId())
                .orderByDesc(Trigger::getId);
        if (trigger.getParams() != null) {
        }
        return queryWrapper;
    }

    @ApiOperation("触发器列表")
    @SaCheckPermission("trigger_view")
    @GetMapping("/list")
    public R list(Page page, Trigger trigger) {
        IPage<Trigger> triggerPage = triggerService.page(page, getQueryWrapper(trigger));
        List<Trigger> list = triggerPage.getRecords();
        for (Trigger trigger1 : list) {
            Table table = tableService.getById(trigger1.getEntityId());
            if (table != null) {
                trigger1.setEntityName(table.getTableComment());
            }
        }
        return R.ok(triggerPage.getRecords(), triggerPage.getTotal());
    }

    @ApiOperation("触发器查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        Trigger trigger = triggerService.getById(id);
        return R.ok(trigger);
    }

    @SysLog("触发器新增")
    @ApiOperation("触发器新增")
    @SaCheckPermission("trigger_add")
    @PostMapping("/save")
    public R save(@Validated @RequestBody Trigger trigger) {
        triggerService.save(trigger);
        return R.ok();
    }

    @SysLog("触发器修改")
    @ApiOperation("触发器修改")
    @SaCheckPermission("trigger_edit")
    @PutMapping("/update")
    public R update(@Validated @RequestBody Trigger trigger) {
        triggerService.updateById(trigger);
        return R.ok();
    }


    @SysLog("触发器删除")
    @ApiOperation("触发器删除")
    @SaCheckPermission("trigger_del")
    @DeleteMapping("/remove/{id}")
    public R remove(@PathVariable("id") Integer[] id) {
        return R.ok(triggerService.removeByIds(Arrays.asList(id)));
    }


    @SaCheckPermission("trigger_export")
    @PostMapping("/export")
    public void export(Trigger trigger) {
        List<Trigger> list = triggerService.list(getQueryWrapper(trigger));
        ExcelUtil<Trigger> util = new ExcelUtil<Trigger>(Trigger.class);
        util.exportExcelResp(list, "触发器数据");
    }

    @GetMapping("/listEntity")
    public R listEntity() {
        LambdaQueryWrapper<Table> queryWrapper = new LambdaQueryWrapper<Table>();
        queryWrapper.select(Table::getId, Table::getTableName, Table::getTableComment);
        queryWrapper.isNotNull(Table::getCode);
        List<Table> tables = tableService.list(queryWrapper);
        return R.ok(tables);
    }
}
